package com.ctg.payroll2.action.empadmin;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.ctg.payroll2.action.BaseAction;
import com.ctg.payroll2.bean.filter.empadmin.CsEmpDimissionFilter;
import com.ctg.payroll2.bean.po.CsEmpDimission;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.empadmin.CsEmpDimissionService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class CsEmpDimissionAction extends BaseAction {
	private static final long serialVersionUID = -5507274507864026940L;

	private CsEmpDimissionService service;

	private CsEmpDimission csEmpDimission;

	private CsEmpDimissionFilter filter;

	private Page result;

	private String empDimssionIds;

	@Override
	protected void commonOperations() {
	}

	@Override
	protected void createEntity() {
		insertOrUpdateDimission();
	}

	@Override
	protected void deleteEntity() {
		try {
			service.deleteEmpDimission(empDimssionIds);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void updateEntity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void queryEntity() {
		if (filter == null) {
			filter = new CsEmpDimissionFilter();
		}
		HttpServletRequest request=ServletActionContext.getRequest();
		SysUser su=(SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
		filter.setCompanyCode(su.getCompanyCode());
		filter.setStartIndex(Parse.toInteger(request.getParameter("start"), 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter("limit"),
				Constants.DEFAULT_PAGE_SIZE));
		result = service.queryEmpDimissionPageByFilter(filter);
	}

	@Override
	protected void retrieveEntity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void exportXLS() {
		// TODO Auto-generated method stub

	}

	/**
	 * @return 返回csEmpDimission
	 */
	public CsEmpDimission getCsEmpDimission() {
		return csEmpDimission;
	}

	/**
	 * @param csEmpDimission
	 *            设置csEmpDimission
	 */
	public void setCsEmpDimission(CsEmpDimission csEmpDimission) {
		this.csEmpDimission = csEmpDimission;
	}

	/**
	 * @return 返回filter
	 */
	public CsEmpDimissionFilter getFilter() {
		return filter;
	}

	/**
	 * @param filter
	 *            设置filter
	 */
	public void setFilter(CsEmpDimissionFilter filter) {
		this.filter = filter;
	}

	/**
	 * @param service
	 *            设置service
	 */
	public void setService(CsEmpDimissionService service) {
		this.service = service;
	}

	/**
	 * @return 返回result
	 */
	public Page getResult() {
		return result;
	}

	/**
	 * @param result
	 *            设置result
	 */
	public void setResult(Page result) {
		this.result = result;
	}

	public String getEmpDimssionIds() {
		return empDimssionIds;
	}

	public void setEmpDimssionIds(String empDimssionIds) {
		this.empDimssionIds = empDimssionIds;
	}

	public String deleteDimission() {
		try {
			service.deleteEmpDimission(empDimssionIds);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public void insertOrUpdateDimission(){
		try {
			csEmpDimission.setCompanyCode(currentUser.getCompanyCode());
			service.updateDimissionEmployee(csEmpDimission);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	

	public String queryByID(){
		HttpServletRequest re = ServletActionContext.getRequest();
		String id=re.getParameter("id");
		try {
			csEmpDimission = service.queryById(Long.parseLong(id));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String updateByStatus(){
		HttpServletRequest re = ServletActionContext.getRequest();
		String id=re.getParameter("id");
		String status=re.getParameter("status");
		String isValid=re.getParameter("isValid");
		String isInjob=re.getParameter("isInjob");
		String employeeId=re.getParameter("employeeId");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("disId", Long.parseLong(id));
		map.put("status", status);
		try {
			csEmpDimission=new CsEmpDimission();
			csEmpDimission.setIsValid(Integer.parseInt(isValid));
			csEmpDimission.setIsInjob(Integer.parseInt(isInjob));
			csEmpDimission.setEmployeeId(Integer.parseInt(employeeId));
			service.updateStatusById(csEmpDimission,map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
